﻿#ifndef __GNUC__
#pragma execution_character_set("utf-8")
#endif

#include "{ $Project }XML.h"
#include <QFile>

namespace ns{ $Project }XML
{
	C{ $Project }XML::C{ $Project }XML()
	{
	}

	C{ $Project }XML::~C{ $Project }XML()
	{

	}

	bool C{ $Project }XML::LoadFromFile(const std::wstring &fileName)
	{
		QFile file(QString::fromStdWString(fileName));
		if (!file.open(QFile::ReadOnly | QFile::Text))
		{
			return false;
		}

		QXmlStreamReader reader(&file);

		reader.readNext();
		while (!reader.atEnd())
		{
			if (reader.isStartElement())
			{
				if (reader.name() == CEVProject::_nodeName_)
				{
					CEVProject _item;
					_item.read(reader);
					{ $Project }.push_back(_item);
				}
				else
					reader.raiseError(QString("Not a %1 file.").arg(CEVProject::_nodeName_));
			}
			else
			{
				reader.readNext();
			}
		}

		file.close();
		if (reader.hasError())
		{
			return false;
		}
		else if (file.error() != QFile::NoError)
		{
			return false;
		}
		return true;
	}

	bool C{ $Project }XML::SaveToFile(const std::wstring &fileName, bool bFormatted)
	{
		QFile file(QString::fromStdWString(fileName));
		if (!file.open(QFile::WriteOnly | QFile::Text))
		{
			return false;
		}

		QXmlStreamWriter writer(&file);

		writer.setAutoFormatting(bFormatted);
		writer.writeStartDocument();

		for (auto &_item : { $Project })
			_item.write(writer);

		writer.writeEndDocument();

		file.close();
		if (file.error())
		{
			return false;
		}
		return true;
	}

}